package org.atguigu.gmall.realtime.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava30.com.google.common.base.CaseFormat;
import org.atguigu.gmall.realtime.common.Constant;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


/**
 * Project:gmallRealTime
 * Package:org.atguigu.gmall.realtime.util
 * Author: ZengHaiFeng
 * CreateTime:2023/4/21 18:50:29
 * Description: Todo
 * Version：1.0
 */
@Slf4j
public class JdbcUtil {
    public static Connection getPhoenixConnection(){
        return getJdbcConnection(Constant.PHOENIX_DRIVER,Constant.PHOENIX_URL,null,null);
    }

    public static Connection getJdbcConnection(String driver, String url, String username, String passwd){
       // load driver.
        try {
            Class.forName(driver);
            // get connection from driverManger.
           return DriverManager.getConnection(url,username,passwd);
        } catch (ClassNotFoundException e) {
            log.error("load phoenix driver fail!");
            throw new RuntimeException("load phoenix driver fail!");
        } catch (SQLException e) {
            log.error("get connection fail!");
            throw new RuntimeException(e + " => get connection fail!");
        }
    }

    public static void closeConnection(Connection connection){
        if ( connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static Connection getMysqlConnection() {
        return getJdbcConnection(
                Constant.MYSQL_DRIVER,
                Constant.MYSQL_URL,
                "root",
                "aaaaaa");
    }

    public static <T> List<T> queryList(
            Connection mysqlConnection,
            String sql,
            Object[] args,
            Class<T> tClass,boolean... isUnderLineToCamel) {
        boolean flag = false;
        if (isUnderLineToCamel.length > 0 ) {
            flag = isUnderLineToCamel[0];
        }
        ArrayList<T> result = new ArrayList<>();
        try {
            PreparedStatement ps = mysqlConnection.prepareStatement(sql);
            // sql 中如果有占位符，给占位符赋值
            for (int i = 0; args != null && i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            // run query
            ResultSet resultSet = ps.executeQuery();
            // 从 resultSet 中 获取结果 的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 遍历 结果
            while (resultSet.next()){
                // 每行封装一个 T 对象
                // 使用反射 创建一个 T 类型对象
                T t = tClass.newInstance();
                // 遍历每一行数据，把多列数据封装到 T对象的属性中
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // 表的列名必须和属性保持一致
                    String columnLabel = metaData.getColumnLabel(i);
                    // 列名用下划线命名，对象是驼峰命名，需要转换
                    if (flag){
                        columnLabel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,columnLabel);
                    }
                    // 获取列值
                    Object object = resultSet.getObject(i);
                    // 封装
                    BeanUtils.setProperty(t,columnLabel,object);
                }
                result.add(t);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
